﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace 窗口停靠屏幕边缘
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            // 获取屏幕宽高（包括缩放因子）
            this.Height = SystemParameters.PrimaryScreenHeight - 50;
            this.Width = 150;
            this.Left = 0;
            this.Top = 0;
            AddTextBlockToStackPanel(Color.FromRgb(76, 175, 80));
            AddTextBlockToStackPanel(Color.FromRgb(33, 150, 243));
            AddTextBlockToStackPanel(Color.FromRgb(251, 192, 45));
            AddTextBlockToStackPanel(Color.FromRgb(255, 69, 0));
            AddTextBlockToStackPanel(Color.FromRgb(25, 118, 210));
            AddTextBlockToStackPanel(Color.FromRgb(142, 36, 170));
            AddTextBlockToStackPanel(Color.FromRgb(32, 160, 241));
            AddTextBlockToStackPanel(Color.FromRgb(71, 203, 252));
            AddTextBlockToStackPanel(Color.FromRgb(233, 66, 66));
        }

        private void AddTextBlockToStackPanel(Color background, string text = null)
        {
            // 创建 TextBlock
            TextBlock textBlock = new TextBlock
            {
                Width = 10,
                Height = 30,
                Text = text, // 设置显示的文本
                Background = new SolidColorBrush(background), // 设置文本颜色
            };
            textBlock.MouseDown += (sender, e) =>
            {
                var t = (TextBlock)sender;
                // MessageBox.Show(t.Background.ToString());
            };

            // 添加到 StackPanel
            MainWindowStackPanel.Children.Add(textBlock);
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                try
                {
                    this.DragMove();
                }
                catch (InvalidOperationException ex)
                {
                    // 打印日志：处理异常，可能是由于不正确的鼠标事件触发
                    Debug.WriteLine(ex.Message);
                }
            }
        }
    }
}
